package com.lagou.edu.service.impl;

import com.lagou.anno.Autowired;
import com.lagou.anno.Service;
import com.lagou.anno.Transactional;
import com.lagou.edu.dao.AccountDao;
import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.TransferService;


/**
 * @author 应癫
 */
@Service
public class TransferServiceImpl implements TransferService {


    @Autowired
    private AccountDao accountDao;


    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);

        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);

        int i = accountDao.updateAccountByCardNo(to);

        if (i > 0) {

            throw new RuntimeException("测试回滚");
        }
        accountDao.updateAccountByCardNo(from);

    }
}
